//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 刘蒙 on 16/1/11.
//  Copyright © 2016年 liumeng. All rights reserved.
//

import UIKit

//MARK: - 1.定义协议
//@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
//    
//    func userWillLogin()
//    func userWillRegister()
//}

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议方法
    //注册
    optional func userWillRegister()
    //登录方法
    func userWillLogin()
}
class VisitorLoginView: UIView {

//MARK: - 定义代理方法
//    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    //定义代理方法
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    
    //MARK: - 按钮的监听事件方法
    
    //按钮回调  - 监听方法
    @objc private func loginBtnDidClick() {
        visitorLoginViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        visitorLoginViewDelegate?.userWillRegister?()
    }
    
    
    //设置一个view的方法
    func setupUIWithInfo(imageName: String?, tipText: String) {
        if let img = imageName{
            
            circleIcon.image = UIImage(named: img)
            houseIcon.hidden = true
            
        }else {
            
            
            
            //这里说明是首页,需要circle旋转起来
            startAnimation()
        }
        
        tipLabel.text = tipText
    }
    
    //circle旋转起来的函数
    func startAnimation() {
        
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        //旋转时间
        base.duration = 5
        
        //yiq一切换回来,circle就不会在旋转了,所以需要不移除图层
        base.removedOnCompletion = false
        //添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //添加按钮的监听事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        addSubview(circleIcon)
        addSubview(houseIcon)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        addSubview(coverIcon)
        
        //设置位置
        //makeConstraints 表示的是添加一条约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        
        coverIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_centerY)
        }
        
        
        houseIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.centerY.equalTo(circleIcon.snp_bottom).offset(16)
            //宽度约束
            make.width.equalTo(225)
            //高度约束
//            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(tipLabel.snp_left).offset(30)
            make.centerY.equalTo(tipLabel.snp_bottom).offset(50)
            make.width.equalTo(80)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(80)
            make.centerX.equalTo(tipLabel.snp_right).offset(-30)
            make.centerY.equalTo(tipLabel.snp_bottom).offset(50)
        }
        //灰度颜色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
//        self.backgroundColor = UIColor(white: <#T##CGFloat#>, alpha: <#T##CGFloat#>)
        
        //设置背景颜色 ,(注意:这个方法失败了..)
        //        self.backgroundColor = UIColor(red: 232, green: 232, blue: 232, alpha: 0.93)
    }
    

    
    
    //懒加载所有的子控件
    //圈圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var houseIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
    lazy var tipLabel: UILabel = {
       let lb = UILabel()
        lb.textAlignment = .Center
        lb.text = "登录后,别人评论你的微博别人评论你的微博,别人评论你的微博"
        lb.textColor = UIColor.darkGrayColor()
        lb.font = UIFont.systemFontOfSize(14)
        lb.numberOfLines = 0
        lb.sizeToFit()
        return lb
    }()
    
    //登陆按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("Login", forState: .Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.sizeToFit()
        return btn
    }()
    
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("Register", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.sizeToFit()
        return btn
    }()
    
    //灰色的覆盖的图片
    lazy var coverIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
